package com.sunday.fangbeibei.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.sunday.fangbeibei.R;


public class CoPopupWindow extends PopupWindow {
	/**
	 * 上下文对象
	 */
	private Context mContext;
	/**
	 * 回调接口对象
	 */
	private OnPopupWindowClickListener listener;
	/**
	 * ArrayAdapter对象
	 */
	@SuppressWarnings("rawtypes")
	private ArrayAdapter adapter;
	/**
	 * ListView的数据源
	 */
	private List<String> list = new ArrayList<String>();
	
	public CoPopupWindow(Context context){
		super(context); 
		mContext = context;
		initView();
	}
	
	private void initView(){
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View popupView = inflater.inflate(R.layout.item_copopupwinsow, null);
		setContentView(popupView);
		
		//设置宽度,若没有设置宽度为LayoutParams.WRAP_CONTENT
		setWidth(LayoutParams.MATCH_PARENT);  
		setHeight(LayoutParams.WRAP_CONTENT);
		
		//设置动画，也可以不设置。不设置则是显示默认的
		//setAnimationStyle(R.style.popupwindow_animation);
		
		//这里很重要，不设置这个ListView得不到相应
		this.setFocusable(true);
	    this.setBackgroundDrawable(new BitmapDrawable()); 
	    this.setOutsideTouchable(true);
	    	
		ListView listView = (ListView) popupView.findViewById(R.id.popupwindow);
		adapter = new ArrayAdapter(mContext, R.layout.item_copopupwinsow_inner, R.id.popup_item, list);
		listView.setAdapter(adapter);
		
		//ListView的点击事件
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CoPopupWindow.this.dismiss();
				if(listener != null){
					listener.onPopupWindowItemClick(position);
				}
			}
		});
		
	}
	
	/**
	 * 为PopupWindow设置回调接口
	 * @param listener
	 */
	public void setOnPopupWindowClickListener(OnPopupWindowClickListener listener){
		this.listener = listener;
	}
	

	/**
	 * 设置数据的方法，供外部调用
	 * @param mList
	 */
	public void changeData(List<String> mList) {
		//这里用addAll也很重要，如果用this.list = mList，调用notifyDataSetChanged()无效
		//notifyDataSetChanged()数据源发生改变的时候调用的，this.list = mList，list并没有发生改变
		list.clear();
		list.addAll(mList);
		adapter.notifyDataSetChanged();
	}
	
	
	/**
	 * 回调接口.供外部调用
	 * @author xiaanming
	 *
	 */
	public interface OnPopupWindowClickListener{
		/**
		 * 当点击PopupWindow的ListView 的item的时候调用此方法，用回调方法的好处就是降低耦合性
		 * @param position 位置
		 */
		void onPopupWindowItemClick(int position);
	}


}
